package com.training.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.training.admin.dao.ProjectPhaseCourseDao;
import com.training.admin.dao.ProjectPhaseDao;
import com.training.admin.dao.ProjectTraineeDao;
import com.training.admin.dao.TrainerDao;
import com.training.admin.entity.course.Course;
import com.training.admin.entity.project.Project;
import com.training.admin.entity.project.ProjectPhase;
import com.training.admin.entity.project.ProjectPhaseCourse;
import com.training.admin.entity.trainee.IdmUser;
import com.training.admin.entity.trainer.Trainer;
import com.training.admin.service.ProjectPhaseCourseService;

/**
 * 
 * @author Rockett
 *
 */
@Service("projectPhaseCourseService")
public class ProjectPhaseCourseServiceImpl extends DataServiceImpl<ProjectPhaseCourse> implements ProjectPhaseCourseService{

	@Dao
    private ProjectPhaseCourseDao projectPhaseCourseDao;
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;

	@Dao
    private ProjectPhaseDao projectPhaseDao;
	
	@Dao
	private ProjectTraineeDao projectTraineeDao;
	
	@Dao
	private TrainerDao trainerDao;
	
	@Override
	public List<ProjectPhaseCourse> listByProjectPhaseId(Long projectPhaseId) {
		return projectPhaseCourseDao.listByProjectPhaseId(projectPhaseId);
	}

    @Override
	public List<ProjectPhaseCourse> listByProjectPhaseAndCourseId(Long projectPhaseId, Long courseId) {
    	return projectPhaseCourseDao.listByProjectPhaseAndCourseId(projectPhaseId,courseId);
    }

	/* (non-Javadoc)
	 * @see com.training.admin.service.ProjectPhaseCourseService#countByProjectId(java.lang.Long)
	 */
	@Override
	public Object[]  statisticsByProjectId(Long projectId) {
		return projectPhaseCourseDao.statisticsByProjectId(projectId);
	}

    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#deleteProjectPhaseCourse(java.lang.Long)
     */
    public void deleteProjectPhaseCourse(Long projectPhaseCourseId) {
        
        ProjectPhaseCourse projectPhaseCourse = genericDaoSupport.load(ProjectPhaseCourse.class, projectPhaseCourseId);
        
        genericDaoSupport.update(projectPhaseCourse.delete());
        
        updateProjectStatus(projectPhaseCourse.getProjectPhase().getProject());
    }
    
    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#updateProjectStatus(com.training.admin.entity.project.Project)
     */
    public void updateProjectStatus(Project project) {

        boolean isAllPhaseCourseAssigned = true;
        List<ProjectPhase> projectPhaseList = projectPhaseDao.listByProjectId(project.getId());
        if (projectPhaseList != null) {
            for (int i = 0; i < projectPhaseList.size(); i++) {
                ProjectPhase projectPhase = projectPhaseList.get(i);
                List<ProjectPhaseCourse> courses = projectPhaseCourseDao.listByProjectPhaseId(projectPhase.getId());
                if (courses != null && courses.size() > 0) {
                    continue;
                } else {
                    isAllPhaseCourseAssigned = false;
                }
            }
        }
        if (!isAllPhaseCourseAssigned) {
            project.setStatus("课程添加未完成");
            genericDaoSupport.update(project);
            return;
        }
        int traineeCount = projectTraineeDao.countByProjectId(project.getId());
        if (traineeCount < 1) {
            project.setStatus("学员添加未完成");
            genericDaoSupport.update(project);
            return;
        }
        project.setStatus("正常");
        genericDaoSupport.update(project);
        return;

    }

    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#createProjectPaseCourse(java.lang.Long, java.lang.Long, com.training.admin.entity.course.Course)
     */
    public void createProjectPaseCourse(Long trainerId, String trainerName, String supplier, String trainerdesc, Long projectPhaseId, Course course) {

    	
        ProjectPhase projectPhase = genericDaoSupport.load(ProjectPhase.class, projectPhaseId);
        Trainer trainer = null;
        if(trainerId != null) {
            IdmUser idmUser = genericDaoSupport.load(IdmUser.class, trainerId);
            
            if(idmUser == null || course == null || projectPhase == null) {
                throw new BusinessException("数据异常");
            }
            
            trainer=trainerDao.loadByEmail(idmUser.getMail());
            if(trainer == null) {
                trainer = new Trainer(idmUser);
                genericDaoSupport.save(trainer);
            }
        } else {
            trainer = new Trainer(trainerName, supplier, trainerdesc);
            genericDaoSupport.save(trainer);
        }
        
        genericDaoSupport.save(course);
        
        ProjectPhaseCourse projectPhaseCourse = new ProjectPhaseCourse(trainer, projectPhase, course);
        genericDaoSupport.save(projectPhaseCourse);
        
        // update project status
        this.updateProjectStatus(projectPhase.getProject());
    }

    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#bindCourse(java.lang.Long, java.util.List)
     */
    public void bindCourse(Long projectPhaseId,  List<Long> selectCourseIds) {
        
        ProjectPhase projectPhase = genericDaoSupport.load(ProjectPhase.class, projectPhaseId);
            for (int i = 0; i < selectCourseIds.size(); i++) {
              Long courseId = selectCourseIds.get(i);
              // check existing course in current project phase
              ProjectPhaseCourse projectPhaseCourse = projectPhaseCourseDao.loadByProjectPhaseAndCourseId(projectPhase.getId(), courseId);

              if (projectPhaseCourse == null) {
                  Course course = genericDaoSupport.load(Course.class, courseId);
                  ProjectPhaseCourse projectPhaseCourse1 = new ProjectPhaseCourse(course.getTrainer(), projectPhase, course);
                  genericDaoSupport.save(projectPhaseCourse1);
              }
        }
            updateProjectStatus(projectPhase.getProject());
    }

    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#listByProjectPhase(com.training.admin.entity.project.ProjectPhase)
     */
    public List<ProjectPhaseCourse> listByProjectPhase(ProjectPhase projectPhase) {
        List<ProjectPhaseCourse> projectPhaseCourses = projectPhaseCourseDao.listByProjectPhase(projectPhase);
        return projectPhaseCourses;
    }

    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#bindTrainer(java.lang.Long, java.lang.Long)
     */
    public void bindTrainer(Long projectPhaseCourseId, Long trainerId) {
        
        ProjectPhaseCourse projectPhaseCourse = genericDaoSupport.load(ProjectPhaseCourse.class, projectPhaseCourseId);
        
        IdmUser idmUser = genericDaoSupport.load(IdmUser.class, trainerId);
        if(idmUser == null || projectPhaseCourse == null) {
            throw new BusinessException("数据异常");
        }
        
        Trainer trainer =trainerDao.loadByEmail(idmUser.getMail());
        if(trainer == null) {
            trainer = new Trainer(idmUser);
            genericDaoSupport.save(trainer);
        }
        
        projectPhaseCourse.editTrainer(trainer);
        genericDaoSupport.update(projectPhaseCourse);
    }

    /* (non-Javadoc)
     * @see com.training.admin.service.ProjectPhaseCourseService#editCourse(java.lang.Long, java.lang.Long, com.training.admin.entity.course.Course)
     */
    public void editCourse(Long projectPhaseCourseId, Long trainerId, Course course) {
        
    	ProjectPhaseCourse projectPhaseCourse = genericDaoSupport.load(ProjectPhaseCourse.class, projectPhaseCourseId);
        IdmUser idmUser = genericDaoSupport.load(IdmUser.class, trainerId);
        
        if (idmUser == null || projectPhaseCourse == null) {
            throw new BusinessException("数据异常");
        }
        
        Trainer trainer = trainerDao.loadByEmail(idmUser.getMail());
        if (trainer == null) {
            trainer = new Trainer(idmUser);
            genericDaoSupport.save(trainer);
        }
        
        genericDaoSupport.update(projectPhaseCourse.editProjectPhaseCourse(trainer, projectPhaseCourse.getCourse().editCourse(course)));
    }
    
	/* (non-Javadoc)
	 * @see com.training.admin.service.CourseService#listByTrainer(java.lang.Long)
	 */
	@Override
	public List<ProjectPhaseCourse> listByTrainer(Long trainerId) {
		return projectPhaseCourseDao.listByTrainer(trainerId);
	}


}
